export function invokeAPi () {
  var a = Array.prototype.slice.call(arguments)
  return window.xz_call && window.xz_call.apply(this, a)
}

export function invokeAsyncAPi () {
  var a = Array.prototype.slice.call(arguments)
  return new Promise((resolve) => {
    a.push((...args) => resolve(args.length > 1 ? args : args[0]))
    window.xz_call && window.xz_call.apply(this, a)
  })
}

export function registerInformApi (apiName, cb) {
  (window.xz_register && window.xz_register(apiName, cb)) || (window[apiName] = cb)
}
export function reportInfo (code, info) {
  invokeAsyncAPi('xz_submit_info_report', code, JSON.stringify(info))
}
/**
 * @param sceneId {Number} 场景id
 * @param reportId {Number} 埋点id
 * @param payload {String|Number} 值
 * @return null
*/
export const reportInfoApi = (sceneId, reportId, payload) => {
  // 参数为字符串类型时
  let methodName = 'information_string_report'
  // 数值类型
  if (/^(-?\d+)(\.\d+)?$/.test(payload)) {
    methodName = 'information_count_report'
  }
  return invokeAsyncAPi(methodName, reportId, sceneId, payload)
}
